package example.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MeiPo implements InvocationHandler {

    /*private Person target;

    public Object getInstance(Person target) throws  Exception{
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), this);
    }
    */

    private Object targetInterface;

    public Object getInstance(Object targetInterface) throws Exception {
        System.out.println("被代理对象" + targetInterface.getClass());
        this.targetInterface = targetInterface;
        return Proxy.newProxyInstance(targetInterface.getClass().getClassLoader(),
                targetInterface.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("我是媒婆，我要开始给你介绍对象了");
        System.out.println("开始物色人选------");

        System.out.println("*-*-*" + (proxy.getClass().getInterfaces()[0]));

        method.invoke(targetInterface, args);

        System.out.println("相亲完毕------");
        System.out.println("合适就准备办事吧");
        return null;
    }
}
